import { Controller, Get, Post, Body, Patch, Param, Delete, UseInterceptors } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './entities/user.entity';
import { ResultData } from 'src/common/response/result';

import { ResponsInterceptoreBase } from '../../../transform.interceptor';

import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { BaseController } from 'src/common/BaseController';
import { QueryListDto } from 'src/common/dto/';

@ApiTags('用户管理')
@Controller('user')
@UseInterceptors(ResponsInterceptoreBase)
export class UserController extends BaseController<User, UserService> {
// Extending BaseController to inherit common functionality
  constructor(private readonly userService: UserService) {
    super(userService); // Pass the service to the base controller
  }

  // @Post()
  // create(@Body() createUserDto: CreateUserDto) {
  //   return this.userService.create(createUserDto);
  // }

  // @Get()
  // findAll() {
  //   return this.userService.findAll();
  // }

  @Post("GetUsers")
  @ApiOperation({ summary: '获取用户列表' })
  async GetUsers(@Body() querylistDto: QueryListDto) {
    return this.Success(await this.userService.findAll(querylistDto), "查询成功");
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    let result = await this.userService.findOne(id);
    
    if (result[0]) {
      return this.Success(result[1], "查询成功");
    } else {
      return this.Fail(result[1], "查询失败");
    }
  }

  @Patch("UpdateUser")
  @ApiOperation({ summary: '用户更新' })
  async update(@Body() updateUserDto: CreateUserDto) {
    let result = await this.userService.update(updateUserDto);
    if(result[0]){
      return this.Success(result[1], "更新成功");
    }else{
      return this.Fail(result[1], "更新失败");
    }
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
  
  @Post('register')
  @ApiOperation({ summary: '用户注册' })
  @ApiResponse({ status: 200, description: '注册成功', type: User })
  async register(@Body() user: CreateUserDto) {
    return this.Success(await this.userService.register(user), "注册成功");
  }
}
